<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\model;

use think\db\Query;
use app\exception\MessageException;
class Room extends BaseModel
{


    protected $name = 'Room';


    const STATUS_DISABLE = 0;
    const SEX_START = 1;

    protected $statusEnum = [
        self::STATUS_DISABLE => '禁用',
        self::SEX_START      => '启用',
    ];

    const SEX_UNKNOWN = 0;
    const SEX_MALE = 1;
    const SEX_FEMALE = 2;

    protected $sexEnum = [
        self::SEX_UNKNOWN => '未知',
        self::SEX_MALE    => '男',
        self::SEX_FEMALE  => '女',
    ];

    /**
     * @Description: 已住人数修改器
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2023-10-19 15:04:26
     * @version: 1.0.0
     * @return 
     */    
    // public function setUsePeopleAttr($value,$data){
    //     halt($value);
    // }
    /**
     * @Description: 关联楼房
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-24 10:20:32
     * @version: 1.0.0
     * @return [type]
     */    
    public function building(){
        
        return $this->belongsTo('building', 'building_number')->bind([
            'building_name' => 'name',
            'building_num'  => 'building_number'
        ]);
    }
    /**
     * @Description: 关联小区表(远程一对多)
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-27 16:47:07
     * @version: 1.0.0
     * @return [type]
     */    
    // public function housing(){
    //     return $this->hasManyThrough('housing', 'building','id','id','building_number','housing_id');
    // }
    /**
     * @Description: 关联房型
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-24 10:37:49
     * @version: 1.0.0
     * @return [type]
     */
    public function roomType(){

        return $this->belongsTo('roomType','room_type')->bind([
            'type_name' => 'name'
        ]);
    }
    /**
     * @Description: 查找房间
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-10-26 16:22:16
     * @version: 1.0.0
     * @return [type]
     */
    public static function getRoom($where){

        $res = self::where($where)->find();

        if (!$res) {
            throw new MessageException('找不到该房间');
        }
        $res->roomType;
        $res->building;

        return $res;
        
    }

    /**
     * @Description: 某个值自增或自减
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @param [type] $value
     * @Date: 2022-10-26 16:34:31
     * @version: 1.0.0
     * @return [type]
     */    
    public static function saveValue($where,$value,$number = 1,$user = null){

        // 
        $res = self::where($where)->value('use_people');
        $sex = 0;
        if($user){
            $sex = User::where('id',$user)->value('sex');
        }
        if($number < 0){
            if($res == intval(abs($number))){
                self::where($where)->save(['sex'=> 0]);
            }
            return self::where($where)->dec($value, intval(abs($number)))->update();
        }else{
            if($res == 0){
                self::where($where)->save(['sex'=>$sex]);
            }
            return self::where($where)->inc($value, intval($number))->update();
        }
        
    }
    /**
     * @Description: 获取房间的租金
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-31 13:37:41
     * @version: 1.0.0
     * @return [type]
     */    
    public function getRoomPrice($where){


        $room = self::where($where)->find();

        $roomPrice = $room->roomType->amount;

        return $roomPrice;

    }

    /**
     * @Description: 获取房间的物业费
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-31 13:37:41
     * @version: 1.0.0
     * @return [type]
     */
    public function getPropertyPrice($where)
    {


        $room = self::where($where)->find();

        $propertyPrice = $room->roomType->property_price;

        return $propertyPrice;
    }

    /**
     * 搜索
     * @param Query $query
     * @param $value
     * @return Query
     */
    public function searchIndexAttr(Query $query, $value)
    {   
        

        return $query->where('building_number|room_type', 'like', '%' . $value . '%');
        // return $query->alias('rm')
        // ->join('wuye_room_type roomType', 'roomType.id = rm.room_type')
        // ->join('wuye_building building', 'building.id = rm.building_number')
        // ->where('building.name|roomType.name', 'like', '%' . $value . '%')->select();
    }
}
